package com.wangbl.infomanager;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.tangye.android.http.Request.ResponseListener;
import com.wangbl.infomanager.bean.CatalogSubkey;
import com.wangbl.infomanager.http.Http;
import com.wangbl.infomanager.http.Response;
import com.wangbl.infomanager.utils.Logger;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CatalogActivity extends BaseActivity implements OnClickListener {

	protected static final String TAG = "CatalogActivity";

	private Http http;

	// private List<E>

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_catalog);
		super.onCreate(savedInstanceState);
		title_text.setText("目录");
		title_user.setVisibility(View.GONE);
		title_back.setVisibility(View.VISIBLE);
		title_menu.setVisibility(View.GONE);

		RelativeLayout parent = (RelativeLayout) findViewById(R.id.catalog_parent);
		for (int i = 0; i < parent.getChildCount(); i++) {
			View view = parent.getChildAt(i);
			view.setOnClickListener(this);
		}
//		doRequest();
	}
	
	private void doRequest() {
		pd.show();
		http = new Http("/rucjson/levea.php", true);
		http.setDebug(Http.isDebug);
		http.asyncRequest(new ResponseListener<Response>() {

			@Override
			public void onErr(int arg0, String arg1, int arg2, Object... arg3) {

			}

			@Override
			public void onResp(int arg0, Response arg1, Object... arg2) {
				Logger.i(TAG, "arg1:" + arg1.json);
				pd.dismiss();
			}

		});
	}

	private List<CatalogSubkey> parser(String content) {
		List<CatalogSubkey> subkeys = new ArrayList<CatalogSubkey>();

		try {
			JSONObject obj = new JSONObject(content);
			JSONObject obj_data = obj.getJSONObject("data");
			JSONArray array_levelone = obj_data.getJSONArray("Levelone");
			JSONObject obj_arr_0 = array_levelone.getJSONObject(0);

			JSONObject obj_0 = obj_arr_0.getJSONObject("0");
			CatalogSubkey subkey = new CatalogSubkey();
			subkey.setColumn(obj_0.getString("column"));
			subkey.setId(obj_0.getString("id"));

			JSONObject obj_arr_1 = array_levelone.getJSONObject(1);

		} catch (JSONException e) {
			Logger.e(TAG, e.toString());
			e.printStackTrace();
		}

		return subkeys;
	}
	
	private String[] itemId = { "260", "261", "284", "285", "279", "280",
			"281", "283", "271", "264", "265", "267", "268", "269",
			"263", "309", "310", "272", "298", "302" };


	@Override
	public void onClick(View v) {
		int index = Integer.valueOf((String) v.getTag());
		Logger.i(TAG, "index:"+index+"     "+itemId[index]);
		Intent intent = new Intent(this, CatalogDetailActivity.class);
		intent.putExtra("id", itemId[index]);
		intent.putExtra("title", ((TextView) v).getText().toString());
		startActivity(intent);
	}

	@Override
	protected void interrupt() {
		if (http != null) {
			http.abort();
		}
	}

}
